package edu.maplef.bookwild.action;

import java.util.List;

import com.xiaonei.api.XiaoneiRestClient;
import com.xiaonei.api.schema.Friend;
import com.xiaonei.api.schema.FriendsGetFriendsResponse;

public class TestAction extends BaseAction {

	private String sessionKey = null;
	private String code = null;

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 8962577520035235765L;

	//	public String test() {
	//		response.setContentType("text/html;charset=utf-8");
	//		response.setCharacterEncoding("utf-8");
	//		response.setHeader("Charset", "utf-8");
	//		response.setHeader("Cache-Control", "no-cache");
	//		try {
	//			request.setCharacterEncoding("UTF-8");
	//		} catch (UnsupportedEncodingException e1) {
	//			e1.printStackTrace();
	//		}
	//
	//		JSONObject json = new JSONObject();
	//
	//		String cmd = StrCharUtil.formatNullStr(request.getParameter("cmd"));
	//		String callback = StrCharUtil.formatNullStr(request
	//				.getParameter("callback"));
	//
	//		sessionKey = request.getParameter("xn_sig_session_key");
	//		code = request.getParameter("code");
	//
	//		XiaoneiRestClient client = new XiaoneiRestClient(
	//				"8267de89014449118881282f28bdf5d9",
	//				"b2ded9e51e2745cb8b02b31ed52224a1", sessionKey);
	//		//client.setDebug(true);
	//
	//		try {
	//			if ("getfriends".equals(cmd)) {
	//				JSONArray jj = JSONArray.fromObject(getFriend(client));
	//				json.put("res", jj.toString());
	//			} else if ("myinfor".equals(cmd)) {
	//				int loggedInUserId = 0;
	//				try {
	//					loggedInUserId = client.users_getLoggedInUser();
	//					EnumSet<ProfileField> enumFields = EnumSet.of(
	//							ProfileField.NAME, ProfileField.HOMETOWN_LOCATION,
	//							ProfileField.BIRTHDAY, ProfileField.HEADURL,
	//							ProfileField.MAINURL, ProfileField.SEX,
	//							ProfileField.STAR, ProfileField.TINYURL,
	//							ProfileField.ZIDOU,
	//							ProfileField.UNIVERSITY_HISTORY,
	//							ProfileField.WORK_HISTORY);
	//
	//					Document doc = client.users_getInfo(loggedInUserId,
	//							enumFields);
	//					json.put("xml", doc.toString());
	//					UsersGetInfoResponse loggedUserGetInfoRes = (UsersGetInfoResponse) client
	//							.getResponsePOJO();
	//					json.put("res", JSONArray.fromObject(loggedUserGetInfoRes
	//							.getUser()));
	//				} catch (XiaoneiException e) {
	//					e.printStackTrace();
	//				}
	//			} else if ("invite".equals(cmd)) {
	//				List<Integer> userIds = new ArrayList<Integer>();
	//				List<Friend> friends = getFriend(client);
	//
	//				for (Friend f : friends) {
	//					userIds.add(f.getId());
	//				}
	//
	//				client.requests_sendRequest(userIds);
	//				request.setAttribute("uids", client.getRawResponse());
	//				//进一步看看都成功邀请了哪些朋友
	//				RequestsSendRequestResponse resp = (RequestsSendRequestResponse) client
	//						.getResponsePOJO();
	//
	//				List<Integer> uids = resp.getUid();
	//
	//				json.put("res", JSONArray.fromObject(uids));
	//			}
	//			if (json != null) {
	//				if (callback.equals("")) { //没有回调函数
	//					response.getWriter().println(json.toString());
	//				} else {
	//					response.setContentType("text/javascript;charset=utf-8");
	//					response.getWriter().println(
	//							callback + "(" + json.toString() + ")");
	//				}
	//			}
	//		} catch (Exception ex) {
	//			ex.printStackTrace();
	//		}
	//		return SUCCESS;
	//	}

	private List<Friend> getFriend(XiaoneiRestClient client) throws Exception {
		client.friends_getFriends();
		FriendsGetFriendsResponse resp = (FriendsGetFriendsResponse) client
				.getResponsePOJO();
		List<Friend> friends = resp.getFriend();
		return friends;
	}

	private String buildUserInfo(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div align=\"center\">\n");
		sb.append("图片：<xn:profile-pic uid=\"");
		sb.append(id);
		sb.append("\" linked=\"true\" size=\"tiny\"/><br>\n");
		sb.append("姓名：<xn:name uid=\"" + id
				+ "\" linked=\"true\" shownetwork=\"true\" />\n");
		sb.append("</div>\n");
		return sb.toString();
	}

}
